//
//  TPDispatchBarrierController.m
//  TEXT
//
//  Created by mac on 2024/5/5.
//  Copyright © 2024 刘博. All rights reserved.
//

#import "TPDispatchBarrierController.h"

@interface TPDispatchBarrierController ()

@end

@implementation TPDispatchBarrierController

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self testBarrier];
   // [self testBarrier2];
    [self textSycMutilThread];
    // Do any additional setup after loading the view.
}

- (void)testBarrier {
    dispatch_queue_t concurrentQueue = dispatch_queue_create("test", DISPATCH_QUEUE_CONCURRENT);
        
        for (NSInteger i = 0; i < 10; i++) {
            
            dispatch_sync(concurrentQueue, ^{
                
                NSLog(@"%zd",i);
            });
        }
        
        dispatch_barrier_sync(concurrentQueue, ^{
           
            NSLog(@"barrier");
        });
    NSLog(@"哈哈哈哈当前线程当前线程当前线程");
        
        for (NSInteger i = 10; i < 20; i++) {
            
            dispatch_sync(concurrentQueue, ^{
                
                NSLog(@"%zd",i);
            });
        }
}


- (void)testBarrier2 {
    dispatch_queue_t concurrentQueue = dispatch_queue_create("test", DISPATCH_QUEUE_CONCURRENT);
        
        for (NSInteger i = 0; i < 10; i++) {
            
            dispatch_sync(concurrentQueue, ^{
                
                NSLog(@"%zd",i);
            });
        }
        
        dispatch_barrier_async(concurrentQueue, ^{
           
            NSLog(@"barrier");
        });
    
    NSLog(@"哈哈哈哈哈这里这里这里这里当前线程");
    
        for (NSInteger i = 10; i < 20; i++) {
            
            dispatch_sync(concurrentQueue, ^{
                
                NSLog(@"%zd",i);
            });
        }
}

- (void)textSycMutilThread
{
    dispatch_queue_t concurrentQueue = dispatch_queue_create("testkkk", DISPATCH_QUEUE_CONCURRENT);
    dispatch_queue_t concurrentQueue3 = dispatch_queue_create("test", DISPATCH_QUEUE_CONCURRENT);

    dispatch_async(concurrentQueue, ^{
        dispatch_sync(concurrentQueue3, ^{
            NSLog(@"111111");
            sleep(10);
            NSLog(@"2222222");
        });
        dispatch_sync(concurrentQueue3, ^{
            NSLog(@"33333");
            sleep(10);
            NSLog(@"444444");
        });
    });
    dispatch_async(concurrentQueue, ^{
        dispatch_sync(concurrentQueue3, ^{
            NSLog(@"55555");
            sleep(10);
            NSLog(@"666666");
        });
    });
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
